$(function() {
	var userInfo = $.cookie('login_cookie');
    if (userInfo != null && userInfo != "") {
		var name = userInfo.split("%")[1];
		$(".header-left").html(
				"欢迎，"+name+"&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' class='link-logout' onclick='logout()'>[退出]</a>"		
		);
	} else {
		window.location.href = ("login.html");
	}
	var f=true;
	var f2=true;
	//注册信息验证
	$("input[name='car_id'],input[name='teacher'],input[name='remarks']").blur(function(){
		var flag =true;
		var flag2=true;
		var car_id=$("input[name='car_id']").val();
		var teacher=$("input[name='teacher']").val();		
		var remarks=$("input[name='remarks']").val();
		if(car_id==""){
			flag=flag && false;
		}
		if(teacher==""){
			flag=flag && false;
		}
		
		f=flag;
		if(!(/^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$/.test(car_id))){ 
			flag2=flag2 && false;
			 }
		f2=flag2;
	});
	
	var search=window.location.search;
	var id=search.substring(1, search.length);
	var car_id=decodeURI(id);
	$.ajax({
		url:"car/selectInfo",
		data:{"car_id":car_id},
		type:"post",
		dataType:"json",
		async:false,
		success:function(data){
			$("input[name='car_id']").val(data.carid);
			$("input[name='teacher']").val(data.teacher);
			$("input[name='remarks']").val(data.remarks);
		}
		
	});
	//为注册按钮绑定点击注册时间  出发regist方法
	$("#payment-button").click(function(){
		if(!f){
			alert("学员信息不能为空！")
		}else if(!f2){
			alert("请输入正确的车牌号！")
		}else{ 
			return update();
		}
	});
})
 function logout() {
		$.ajax({
			url : "user/logout",
			type : "post",
			dateType : "json",
			success : function(data) {
				window.location.reload();
			},
			error : function(e) {
				alert("请求失败！");
			}
		})
}
function update(){
	var search=window.location.search;
	var id=search.substring(1, search.length);
	var info=decodeURI(id);
	var car_id=$("input[name='car_id']").val();
	var teacher=$("input[name='teacher']").val();		
	var remarks=$("input[name='remarks']").val();
	
	$.ajax({
		url:"car/update",
		data:{"car_id":car_id,"teacher":teacher,"remarks":remarks,"info":info},
		type:"post",
		dataType:"json",
		async:false,
		success:function(data){
			var flag=data.success;
			if(flag){
				alert("修改成功！");
				window.location.href="select-car.html";
			}
		},
		error:function(e){
			alert("修改失败！");
			window.location.href="add-car.html";
		}
	})
}